*UserToPerson control class*/
$N.ctrl.UsersCtrl = (function(){
    var notify;
    
	function getUserInfo() {
		var userName = $N.util.getUserName();
		$N.databaseSet.UsersTable.getUserInfo(userName, _sqlResponseCb);
	}
	function _downloadUserInfo() {
		var postURL = "http://kangbox.cn/cakephp/mobileUsersAdapter/mobileusertopersonsdown";
		var para = {
			jsonstring : JSON.stringify({
				username : "hello"
			})
		};
		$N.util.ajaxPost(postURL, para, _ajaxResponseCb);
	}
    
    function _sqlResponseCb(tx, result) {
		if (result.rows.length != 0) {
			for (var i = 0; i < result.rows.length; i++) {
                notify(result.rows.item(i));
			}
		} else {
			downloadUserInfo();
		}
	}

    function _ajaxResponseCb(jsonResponse) {
		if (jsonResponse.length == 0) {
			//put a placeholder
			console.log("json_response string is empty.");
		} else {
			if (jsonResponse.search("NO MORE") != -1) {
				return;
			}
			jsonObject = JSON.parse(jsonResponse);
			//save data into the database
			for (var i = 0; i < jsonObject.length; i++) {
				var data = jsonObject[i].UserToPerson;
				$N.databaseSet.UserToPersonTable.addUserToPersonData(data);
                /*get personProfile data*/
                notify(data);
			}
		}
	}
    
    function registerClientNotify(notifyCb) {
        notify = notifyCb;
    }
    
    return {
        getUserInfo : getUserInfo,
        registerClientNotify: registerClientNotify,
        downloadUserInfo : downloadUserInfo,
    };
}());